package cn.sgrand.ioc.service.impl;

import cn.sgrand.ioc.dao.UserMapper;
import cn.sgrand.ioc.service.UserService;
import cn.sgrand.pojo.User;
import cn.sgrand.pojo.UserExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by ggj on 2016/12/6.
 */
@Service
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userDao;

    @Override
    public User findUserByusername(String number) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andNumberEqualTo(number);
        List<User> users = userDao.selectByExample(userExample);
        if(!users.isEmpty()||users.size()>0)
            return users.get(0);
        return null;
    }

    @Override
    public boolean save(User user) {
        String number=user.getNumber();
        User users = findUserByusername(number);
        if(users==null){
            int count = userDao.insert(user);
            if(count==1)
                return true;
            return false;
        }
        if(users!=null){
            int i = userDao.updateByPrimaryKey(user);
            if(i==1)
                return true;
            return false;
        }
        return false;
    }

}
